/**
 * <p>测试报告</p>
 * @author huangjian
 * @createDate 2016-09-12 下午 16:58:58
 * @version V1.0
 */
CommonUtils.regNamespace("report", "index");
report.index = (function(){
	//定义全局变量
	var _params = {
		result:"",//任务结果
		buildId:"", //任务id
		environmentUrl:"",
		environmentName:""
	};
	var _getUnitReportPage = function(){
		var userName = $("#userName").val();
		var userPassword = $("#userPassword").val();
		/*if(userName==null||userName==""){
			alert("请输入用户名");
			return;
		}
		if(userPassword==null||userPassword==""){
			alert("请输入用户密码");
			return;
		}*/
		var params = {
				"userName":userName,
				"userPassword":userPassword
				};
		var path= contextPath+"/report/getUnitReportPage";
		window.location.href = path;
		/*$.ajax({
	        type:"POST",
	        url:contextPath,
	        data:JSON.stringify(params),
	        contentType:"application/json; charset=utf-8",
	        dataType: "json",
	        async:false,
	        success:function(respone){
	        	if(respone){
	        		if(respone.code==0){
	        			//将用户的信息放在全局变量中
	        			user.info.userdata.userName = respone.data.userName;
	        			user.info.userdata.userId = respone.data.userId;
	        			user.info.userdata.roleIds = respone.data.roleIds;
	        			window.location.href = path+"/user/home";
	        		}else{
	        			Lobibox.alert('error', {
	                        msg: respone.errorsList
	                    });
	        			alert(respone.errorsList);
	        		}
	        	}else{
	        		alert("登录失败");
	        	}
	        },error:function(respone){
	            alert("请求发生错误:"+respone);
	        }
	    });*/
	};
	
	var _getInterReportPage = function(){
		var userName = $("#userName").val();
		var userPassword = $("#userPassword").val();
		/*if(userName==null||userName==""){
			alert("请输入用户名");
			return;
		}
		if(userPassword==null||userPassword==""){
			alert("请输入用户密码");
			return;
		}*/
		var params = {
				"userName":userName,
				"userPassword":userPassword
				};
		var path= contextPath+"/report/getInterReportPage";
		window.location.href = path;
	};
	var _triggerKey = function(){
		
		var userName = $("#userName").val();
		var userPassword = $("#userPassword").val();
		var environmentId = $("#ucEnvironmentType").find("option:selected").val();
		var ucEnvironmentDesc = $("#ucEnvironmentType").find("option:selected").text();
		var environmentUrl = $("#ucEnvironmentType").find("option:selected").attr("id");
		var environmentName = $("#environmentName").find("option:selected").attr("name");
		var params = {
				"userName":userName,
				"environmentId":environmentId,
				"ucEnvironmentDesc":ucEnvironmentDesc,
				"environmentUrl":environmentUrl,
				"environmentName":environmentName
				};
		var path= contextPath+"/report/triggerKey";
		$.ajax({
		    type:"POST",
		    url:path,
		    data:JSON.stringify(params),
		    contentType:"application/json; charset=utf-8",
		    dataType: "json",
		    async:false,
		    success:function(respone){
		    	if(respone){
		    		if(respone.code==0){
		    			//将用户的信息放在全局变量中
		    			Lobibox.alert('success', {
		                    msg: respone.msg
		                });
		    			report.index.params.buildId = respone.currentBuildId;
		    			report.index.params.environmentUrl = respone.environmentUrl;
		    			report.index.params.environmentName = respone.environmentName;
		    			report.index.refreshState(report.index.params.buildId,report.index.params.environmentUrl,respone.environmentName);
		    		}else{
		    			Lobibox.alert('error', {
		                    msg: respone.msg
		                });
		    		}
		    	}else{
		    		alert("请求失败");
		    	}
		    },error:function(respone){
		        alert("请求发生错误:"+respone);
		    }
	    });
	}
	var _refreshState = function(buildId,environmentUrl,environmentName){
		var path= contextPath+"/report/refreshState";
		var params = {
				"buildId":buildId,
				"environmentUrl":environmentUrl
				};
		var interval;
		$.ajax({
		    type:"POST",
		    url:path,
		    async:true,
		    data:JSON.stringify(params),
		    contentType:"application/json; charset=utf-8",
		    dataType: "json",
		    async:false,
		    success:function(respone){
		    	if(respone){
		    		if(respone.code==0){
		    			//更新界面状态信息
		    			report.index.params.building = respone.building;
		    			report.index.params.result = respone.result;
		    			var html = "";
		    			$.each(respone.jobList,function(index,job){
		    				html = html+"<tr>"
		    				html=html+"<td class='project-status'><span class='label label-primary'>当前状态："+job.jobStatus+"</td><td class='project-title'> <small>创建于 "+job.createTime+"</small></td>";
		    				html=html+"<td class='project-status'><small id='uresult'>当前结果："+job.jobDesc+"</small></td><td class='project-actions'><a href='#' class='btn btn-white btn-sm'><i class='fa fa-folder'></i> 查看 </a></td>";
		    				html=html+"</tr>";
		    			});
		    			/*if(respone.result=="SUCCESS"){
		    				html=html+"<td class='project-status'><span class='label label-primary'>当前状态："+respone.result+"</td><td class='project-title'> <small>创建于 "+respone.job.createTime+"</small></td>";
		    				html=html+"<td class='project-status'><small id='uresult'>当前结果："+respone.job.jobDesc+"</small></td><td class='project-actions'><a href='#' class='btn btn-white btn-sm'><i class='fa fa-folder'></i> 查看 </a></td>";
		    			}else{
		    				html=html+"<td class='project-status'><span class='label label-primary'>当前状态：building...</td><td class='project-title'> <small>创建于 "+respone.job.createTime+"</small></td>";
		    				html=html+"<td class='project-status'><small id='uresult'>当前结果："+respone.job.jobDesc+"</small></td><td class='project-actions'><a href='#' class='btn btn-white btn-sm'><i class='fa fa-folder'></i> 查看 </a></td>";
		    			}*/
		    			$("#jobBody").html("");
		    			$("#jobBody").html(html);
		    			/*if(respone.building=="true"&&respone.result!="SUCCESS"){
		    				report.index.refreshState(report.index.params.buildId,report.index.params.ucEnvironmentType);
		    				console.log("循环");
		    			}*/
		    			//定时循环
		    			interval = setInterval(function(){
		    				if(report.index.params.result =="SUCCESS"){
		    					clearInterval(interval);
		    					console.log("结束");
		    				}
		    				if(respone.building=="true"){
		    					report.index.refreshState(buildId,environmentUrl);
		    				}
		    			}, 120000);
		    		}else if(respone.code=="-1"){
		    			report.index.refreshState(report.index.params.buildId,report.index.params.environmentUrl);
		    		}else{
		    			Lobibox.alert('error', {
		                    msg: respone.msg
		                });
		    		}
		    	}else{
		    		alert("请求失败");
		    	}
		    },error:function(respone){
		        alert("请求发生错误:"+respone);
		    }
	    });
	}
	return{
		getUnitReportPage:_getUnitReportPage,
		getInterReportPage:_getInterReportPage,
		triggerKey:_triggerKey,
		refreshState:_refreshState,
		params:_params
	};
})();


$(function(){
	//基于准备好的dom，初始化echarts实例
	//获取接口测试的数据信息
	 var usuccess = echarts.init(document.getElementById('usuccess'));
	 var aisuccess = echarts.init(document.getElementById('aisuccess'));
	 //获取接口测试的数据信息
	 var isuccess = echarts.init(document.getElementById('isuccess'));
	 var uisuccess = echarts.init(document.getElementById('uisuccess'));
	 
	 var svalue = 315;
	 var fvalue = 215;
	 
		var woption = {
		   tooltip : {
		       trigger: 'item',
		       formatter: "{a} <br/>{b} : {c} ({d}%)"
		   },
		   emphasis: {
		        color: 'rgba(0,0,0,0)'
		    },
		   color:
		   	['#ccc','#ffa500']
		   	,
		   title:{
		   	text:'一周成功率分布',
		   	x:'center',
		   	textStyle:{
		   		fontSize: 10,
		   	    fontWeight: 'bolder',
		   	    color: '#333'
		   	}
		   },
		   toolbox: {
		       show : false,
		       feature : {
		           mark : {show: true},
		           dataView : {show: true, readOnly: false},
		           magicType : {
		               show: true, 
		               type: ['pie', 'funnel'],
		               option: {
		                   funnel: {
		                       x: '25%',
		                       width: '50%',
		                       funnelAlign: 'center',
		                       max: 1548
		                   }
		               }
		           },
		           restore : {show: true},
		           saveAsImage : {show: true}
		       }
		   },
		   calculable : true,
		   series : [
		       {
		           name:'一周成功率',
		           type:'pie',
		           radius : ['50%', '70%'],
		           itemStyle : {
		               normal : {
		                   label : {
		                       show : false
		                   },
		                   labelLine : {
		                       show : false
		                   }
		               },
		               emphasis : {
		                   label : {
		                       show : true,
		                       position : 'center',
		                       textStyle : {
		                           fontSize : '15',
		                           fontWeight : 'bold'
		                       }
		                   }
		               }
		           },
		           data:[
		                 {value:fvalue, name:'fail'},
		               {value:svalue, name:'success'}
		               
		           ]
		       }
		   ]
		};

		var toption = {
				   tooltip : {
				       trigger: 'item',
				       formatter: "{a} <br/>{b} : {c} ({d}%)"
				   },
				   color:
				   	['#ccc','#40e0fe']
				   	,
				   title:{
				   	text:'总成功率',
				   	x:'center',
				   	textStyle:{
				   		fontSize: 10,
				   	    fontWeight: 'bolder',
				   	    color: '#333'
				   	}
				   },
				   toolbox: {
				       show : false,
				       feature : {
				           mark : {show: true},
				           dataView : {show: true, readOnly: false},
				           magicType : {
				               show: true, 
				               type: ['pie', 'funnel'],
				               option: {
				                   funnel: {
				                       x: '25%',
				                       width: '50%',
				                       funnelAlign: 'center',
				                       max: 1548
				                   }
				               }
				           },
				           restore : {show: true},
				           saveAsImage : {show: true}
				       }
				   },
				   calculable : true,
				   series : [
				       {
				           name:'总成功率',
				           type:'pie',
				           radius : ['50%', '70%'],
				           itemStyle : {
				               normal : {
				                   label : {
				                       show : false
				                   },
				                   labelLine : {
				                       show : false
				                   }
				               },
				               emphasis : {
				                   label : {
				                       show : true,
				                       position : 'center',
				                       textStyle : {
				                           fontSize : '15',
				                           fontWeight : 'bold'
				                       }
				                   }
				               }
				           },
				           data:[
				               {value:fvalue, name:'fail'},
				               {value:svalue, name:'success'}
				               
				           ]
				       }
				   ]
				};
		
	// 使用刚指定的配置项和数据显示图表。
	 isuccess.setOption(woption);
	 uisuccess.setOption(toption);
	 usuccess.setOption(woption);
	 aisuccess.setOption(toption);
	 
	 
	    // 路径配置
       require.config({
           paths: {
               echarts: 'http://echarts.baidu.com/build/dist'
           }
       });
       
       // 使用
       require(
           [
               'echarts',
               'echarts/chart/line' // 使用柱状图就加载bar模块，按需加载
           ],
           function (ec) {
               // 基于准备好的dom，初始化echarts图表
               var iweek = ec.init(document.getElementById('iweek')); 
               var uweek = ec.init(document.getElementById('uweek')); 
               
               var uoption = {
                   tooltip: {
                       show: true
                   },
                   color:
                   	['#b8860b']
                   	,
                   legend: {
                       data:['一周通过率曲线']
                   },
                   xAxis : [
                       {
                           type : 'category',
                           data : ["09-12","09-13","09-14","09-15","09-16"]
                       }
                   ],
                   yAxis : [
                       {
                           type : 'value'
                       }
                   ],
                   series : [
                       {
                           "name":"通过率",
                           "type":"line",
                           "data":[0.85, 0.90, 0.84, 0.74, 0.94]
                       }
                   ]
               };
               var ioption = {
                       tooltip: {
                           show: true
                       },
                       color:
                       	['#4cae4c']
                       	,
                       legend: {
                           data:['一周通过率曲线']
                       },
                       xAxis : [
                           {
                               type : 'category',
                               data : ["09-12","09-13","09-14","09-15","09-16"]
                           }
                       ],
                       yAxis : [
                           {
                               type : 'value'
                           }
                       ],
                       series : [
                           {
                               "name":"通过率",
                               "type":"line",
                               "data":[0.45, 0.60, 0.94, 0.14, 0.74]
                           }
                       ]
                   };
               // 为echarts对象加载数据 
               uweek.setOption(uoption); 
               iweek.setOption(ioption); 
           }
       );
       //查询最后一次拨测的记录
   		/*var path= contextPath+"/report/queryLastTest";
   		var params = {};
   		$.ajax({
   		    type:"POST",
   		    url:path,
   		    data:JSON.stringify(params),
   		    contentType:"application/json; charset=utf-8",
   		    dataType: "json",
   		    async:false,
   		    success:function(respone){
   		    	if(respone){
   		    		if(respone.code==0){
   		    			//定时循环
   		    			var html = "<tr>"
   		    				html=html+"<td class='project-status'><span class='label label-primary'>"+respone.job.jobStatus+"</td><td class='project-title'> <small>创建于 "+respone.job.createTime+"</small></td>";
   		    				html=html+"<td class='project-completion'><small id='uresult'>当前状态："+respone.job.jobDesc+" </small></td><td class='project-actions'><a href='#' class='btn btn-white btn-sm'><i class='fa fa-folder'></i> 查看 </a></td>";
	   		    			html = html+"</tr>";
	   		    			$("#jobBody").html(html);
   		    		}else{
   		    			Lobibox.alert('error', {
   		                    msg: respone.msg
   		                });
   		    		}
   		    	}else{
   		    		alert("请求失败");
   		    	}
   		    },error:function(respone){
   		        alert("请求发生错误:"+respone);
   		    }
   	    });*/
})